package com.jd.risk.bankorder.controller.privilege;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.risk.bankorder.common.CommonResult;
import com.jd.risk.bankorder.controller.BaseController;
import com.jd.risk.bankorder.entity.privilege.Role;
import com.jd.risk.bankorder.service.privilege.RoleService;

@Controller
@RequestMapping("/privilege/role")
public class RoleController extends BaseController{
	
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/toRolePage")
	public String toRolePage()
	{
		return "/privilege/roleListPage";
	}
	
	@ResponseBody
	@RequestMapping("/queryRoles")
	public String queryRoles()
	{
		CommonResult<Role> roles = roleService.getAllRole();
		return toJson(roles);
	}
	
	@RequestMapping("toRoleAuthorization")
	public String toRoleAuthorization(ModelMap modelMap ,@RequestParam(value="roleId") String roleId)
	{
		modelMap.put("roleId", roleId);
		return "/privilege/roleAuthorization";
	}
	
	@RequestMapping("/toRoleAddPage")
	public String toRoleAddPage()
	{
		return "/privilege/roleAddPage";
	}
	
	@ResponseBody
	@RequestMapping("/roleAdd")
	public String roleAdd(Role role)
	{
		CommonResult<String> result = new CommonResult<String>();
		int ret = 	roleService.roleAdd(role);
		
		if(ret>0)
		{
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			result.setMessage("角色添加失败");
		}
		return toJson(result);
	}
	
	@ResponseBody
	@RequestMapping("/authorizate")
	public String authorizate(@RequestParam(value="roleId") Long roleId , @RequestParam(value="resourceIds") String resourceIds)
	{
		int res = roleService.authorizate(roleId,resourceIds);
		CommonResult<String> result = new CommonResult<String>();
		if(res >0)
		{
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			result.setMessage("授权失败！");
		}
		
		return toJson(result);
	}
}
